package com.hantanger.weblog.service;

import java.util.List;

import com.hantanger.weblog.dao.SettingDao;
import com.hantanger.weblog.dao.UserDao;
import com.hantanger.weblog.model.Setting;
import com.hantanger.weblog.model.User;
import com.hantanger.weblog.util.ClassFactory;

public class UserService {
	
	public SettingDao getSettingDao(){
		return ClassFactory.getInstance(SettingDao.class);
	}
	
	public UserDao getUserDao(){
		return ClassFactory.getInstance(UserDao.class);
	}
	
	public Setting getSetting(String name){
		return getSettingDao().getSettingByName(name);
	}
	
	public Setting getDefaultSetting(){
		return getSettingDao().getSettingByName("default");
	}
	
	public void updateSetting(Setting setting){
		getSettingDao().update(setting);
	}
	
	public void insertUser(User user) {
		getUserDao().update(user);
	}

	public void updateUser(User user) {
		getUserDao().update(user);
	}

	public void deleteUser(String email) {
		getUserDao().deleteByEmail(email);
	}

	public User getUser(String email) {
		return getUserDao().getUserByEmail(email);
	}
	
	/**
	 * 根据email,和password获取用户id
	 * @param email
	 * @param password
	 * @return -1表示email对应的用户不存在，0表示email和password不匹配，大于0表示用户id
	 */
	public long getUserId(String email, String password){
		long userId = -1;
		User user = getUserDao().getUserByEmail(email);
		if(user != null){
			if(user.getPassword().equals(password)){
				userId = user.getId();
			}else{
				userId = 0;
			}
		}
		return userId;
	}
	
	public List<User> getAllUser(){
		return getUserDao().getAllUser();
	}
	
}
